package ${package.Controller};

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import ${package.Service}.${table.serviceName};
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.PermissionMeta;
import javax.validation.constraints.Positive;
import java.util.List;
import io.github.talelin.latticy.dto.${table.entityPath}.CreateOrUpdate${entity}DTO;
import ${package.Entity}.${entity}DO;

/**
*
* @author vvboy
*/
@RestController
@RequestMapping("/v1/${table.entityPath}")
@Validated
public class ${table.controllerName} {

    @Autowired
    private ${table.serviceName} ${table.entityPath}Service;

    @GetMapping("/{id}")
    public ${entity}DO get${entity}(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        ${entity}DO ${table.entityPath} = ${table.entityPath}Service.getById(id);
        if (${table.entityPath} == null) {
            throw new NotFoundException("找不到", 10022);
        }
        return ${table.entityPath};
    }

    @GetMapping("")
    public List<${entity}DO> get${entity}s() {
        return ${table.entityPath}Service.findAll();
    }


    @PostMapping("")
    public CreatedVO create${entity}(@RequestBody @Validated CreateOrUpdate${entity}DTO validator) {
    ${table.entityPath}Service.create${entity}(validator);
        return new CreatedVO(12);
    }


    @PutMapping("/{id}")
    public UpdatedVO update${entity}(@PathVariable("id") @Positive(message = "{id.positive}") Integer id, @RequestBody @Validated CreateOrUpdate${entity}DTO validator) {
    ${entity}DO ${table.entityPath} = ${table.entityPath}Service.getById(id);
        if (${table.entityPath} == null) {
        throw new NotFoundException(10022);
        }
    ${table.entityPath}Service.update${entity}(${table.entityPath}, validator);
        return new UpdatedVO(13);
    }


    @DeleteMapping("/{id}")
    @GroupRequired
    @PermissionMeta(value = "删除${table.comment}", module = "${table.comment}")
    public DeletedVO delete${entity}(@PathVariable("id") @Positive(message = "{id.positive}") Integer id) {
    ${entity}DO ${table.entityPath} = ${table.entityPath}Service.getById(id);
    if (${table.entityPath} == null) {
    throw new NotFoundException(10022);
    }
    ${table.entityPath}Service.deleteById(${table.entityPath}.getId());
    return new DeletedVO(14);
    }

}